package com.sc.nft.dao;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.DaoWallet;
import com.sc.nft.entity.MallDaoWallet;
import com.sc.nft.entity.vo.DaoWalletVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.mapper.DaoWalletMapper;
import com.sc.nft.mapper.MallDaoWalletMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;

/**
 * 商城dao钱包
 *
 * @author liGen
 * @version 1.0
 * @date 2024/1/19 3:54 下午
 */
@Slf4j
@Repository
public class MallDaoWalletDao extends ServiceImpl<MallDaoWalletMapper, MallDaoWallet> {

    public MallDaoWallet getByDaoId(Long daoId) {
        MallDaoWallet mallDaoWallet = lambdaQuery().eq(MallDaoWallet::getDaoId, daoId).one();
        Assert.notNull(mallDaoWallet, () -> new GlobalRunTimeException("DAO钱包不存在" ));
        return mallDaoWallet;
    }

    public MallDaoWallet getByDaoIdNotException(Long daoId) {
        MallDaoWallet mallDaoWallet = lambdaQuery().eq(MallDaoWallet::getDaoId, daoId).one();
        return mallDaoWallet;
    }

    /**
     * 添加余额
     *
     * @param daoId
     * @param amount
     */
    public void addBalance(Long daoId, BigDecimal amount) {
        UpdateWrapper<MallDaoWallet> wrapper = new UpdateWrapper<>();
        wrapper.setSql("sum_income=sum_income+" + amount + " , balance=balance+" + amount);
        wrapper.eq("dao_id", daoId);
        update(wrapper);
    }

    /**
     * 添加活动余额
     *
     * @param daoId
     * @param amount
     */
    public void addActivityBalance(Long daoId, BigDecimal amount) {
        UpdateWrapper<MallDaoWallet> wrapper = new UpdateWrapper<>();
        wrapper.setSql("sum_income=sum_income+" + amount + " , activity_balance=activity_balance+" + amount);
        wrapper.eq("dao_id", daoId);
        update(wrapper);
    }

    public void pass(Long daoId, BigDecimal amount) {
        UpdateWrapper<MallDaoWallet> wrapper = new UpdateWrapper<>();
        wrapper.setSql("freeze_amount=freeze_amount-" + amount);
        wrapper.eq("dao_id", daoId);
        update(wrapper);
    }

    public void approval(Long daoId, BigDecimal amount) {
        UpdateWrapper<MallDaoWallet> wrapper = new UpdateWrapper<>();
        wrapper.setSql("freeze_amount=freeze_amount+" + amount + " , balance=balance-" + amount);
        wrapper.eq("dao_id", daoId);
        update(wrapper);
    }

    public void refuse(Long daoId, BigDecimal amount) {
        UpdateWrapper<MallDaoWallet> wrapper = new UpdateWrapper<>();
        wrapper.setSql("freeze_amount=freeze_amount-" + amount + " , balance=balance+" + amount);
        wrapper.eq("dao_id", daoId);
        update(wrapper);
    }

    /**
     * 减少余额
     *
     * @param daoId
     * @param amount
     */
    public void subBalance(Long daoId, BigDecimal amount) {
        UpdateWrapper<MallDaoWallet> wrapper = new UpdateWrapper<>();
        wrapper.setSql("balance=balance -" + amount);
        wrapper.eq("dao_id", daoId);
        update(wrapper);
    }
}